﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewStart
{
    public partial class MenuForm : Form
    {
        public bool openAppForm = false;

        public MenuForm()
        {
            InitializeComponent();
            this.Top = Screen.PrimaryScreen.Bounds.Height - (Screen.PrimaryScreen.Bounds.Height - Screen.PrimaryScreen.WorkingArea.Height) - this.Height;

            label2.Text = MetroHelper.CircledForwardIcon;
            label3.Text = MetroHelper.PowerIcon;

            this.MouseWheel += MenuForm_MouseWheel;
            MouseHelper.MouseClick += DxHelper_MouseClick;
        }
        
        public void LoadTiles()
        {
            try
            {
                foreach (TileInfo tile in SettingsHelper.settings.tiles)
                    TilesHelper.AddTile(tile);
            }
            catch { }
        }

        public void SaveTiles()
        {
            try
            {
                SettingsHelper.settings.tiles = new List<TileInfo>();
                foreach (Tile tile in flowLayoutPanel1.Controls)
                {
                    SettingsHelper.settings.tiles.Add(new TileInfo() { largetile = tile.LargeTile, name = tile.Text, path = tile.path });
                }
            }
            catch { }
        }

        private void DxHelper_MouseClick(object sender, MouseEventArgs e)
        {
            if (PointsHelper.OutMenu(e.Location, this) && !contextMenuStrip1.Visible)
                this.Invoke(new ThreadStart(Close));
        }

        private void MenuForm_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta >= 1)
            {
                AnimationHelper.MoveTiles(flowLayoutPanel1, 50, true);
            }

            if (e.Delta <= -1)
            {
                AnimationHelper.MoveTiles(flowLayoutPanel1, -50, true);
            }
        }

        private void flowLayoutPanel1_DragDrop(object sender, DragEventArgs e)
        {
            Tile data = (Tile)e.Data.GetData(typeof(Tile));

            Point p = flowLayoutPanel1.PointToClient(new Point(e.X, e.Y));
            var item = flowLayoutPanel1.GetChildAtPoint(p);
            int index = flowLayoutPanel1.Controls.GetChildIndex(item, false);
            flowLayoutPanel1.Controls.SetChildIndex(data, index);

            flowLayoutPanel1.Invalidate();
            TilesHelper.ReloadLayout();
        }

        private void flowLayoutPanel1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.Close();
            openAppForm = true;
        }

        private void MenuForm_Shown(object sender, EventArgs e)
        {
            MouseHelper.Enabled = true;
        }

        private void MenuForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MouseHelper.Enabled = false;
            SaveTiles();
            SettingsHelper.SaveSettings();
        }

        private void MenuForm_Load(object sender, EventArgs e)
        {
            CycleForm.a.back = this.BackColor;
            if (flowLayoutPanel1.Controls.Count == 0)
                LoadTiles();
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            SaveTiles();
            CycleForm.Stop();
        }

        private void label3_MouseClick(object sender, MouseEventArgs e)
        {
            contextMenuStrip1.Show(Cursor.Position);
        }

        private void turnOffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveTiles();
            SettingsHelper.SaveSettings();
            MouseHelper.Dispose();
            PowerHelper.ExitWindows(RestartOptions.PowerOff, false);
        }

        private void rebootToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveTiles();
            SettingsHelper.SaveSettings();
            MouseHelper.Dispose();
            PowerHelper.ExitWindows(RestartOptions.Reboot, false);
        }

        private void lockScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SaveTiles();
            SettingsHelper.SaveSettings();
            MouseHelper.Dispose();
            PowerHelper.ExitWindows(RestartOptions.LogOff, false);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new About().Show();
            this.Close();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new SettingsForm().Show();
            this.Close();
        }
    }
}
